package com.lovo.frame;

import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * 取款
 */
public class GetPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel moneyLabel = new JLabel("请输入取款金额");
    private JTextField moneyTextField = new JTextField();
    private JButton sureBtn = new JButton(Util.getIcon("/images/buttonSure.jpg"));
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private Font font = new Font("宋体", Font.BOLD, 25);
    //创建服务层对象
    private UserService userService = new UserServiceImpl();

    public GetPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);//设置成透明

        moneyLabel.setBounds(300, 200, 200, 50);
        moneyLabel.setFont(font);
        this.add(moneyLabel);

        moneyTextField.setBounds(350, 250, 200, 50);
        moneyTextField.setFont(font);
        this.add(moneyTextField);


        sureBtn.setBounds(350, 350, 140, 50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        backBtn.setBounds(520, 350, 140, 50);
        backBtn.addActionListener(this);
        this.add(backBtn);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            // 当期登录的账户
            UserDO loginUser = frame.getLoginUser();
            // 获取输入的金额
            double balance = 0;
            try {
                balance = Double.parseDouble(moneyTextField.getText());
                userService.getMoney(loginUser.getId(), balance);
                JOptionPane.showMessageDialog(this, "取款成功");
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "金额必须为数字");
                return;
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, exception.getMessage());
            }
            frame.switchPanel("menu");

        } else if (e.getSource() == backBtn) {//返回主菜单
            frame.switchPanel("menu");

        }

    }
}
